home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 16 windows forms / otherobjects / sendkeysform.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  4.7 KB  |  127 lines

  1. Public Class SendKeysForm
  2.     Inherits System.Windows.Forms.Form
  3.  
  4. #Region " Windows Form Designer generated code "
  5.  
  6.     Public Sub New()
  7.         MyBase.New()
  8.  
  9.         'This call is required by the Windows Form Designer.
  10.         InitializeComponent()
  11.  
  12.         'Add any initialization after the InitializeComponent() call
  13.  
  14.     End Sub
  15.  
  16.     'Form overrides dispose to clean up the component list.
  17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  18.         If disposing Then
  19.             If Not (components Is Nothing) Then
  20.                 components.Dispose()
  21.             End If
  22.         End If
  23.         MyBase.Dispose(disposing)
  24.     End Sub
  25.     Friend WithEvents Label1 As System.Windows.Forms.Label
  26.     Friend WithEvents txtApplication As System.Windows.Forms.TextBox
  27.     Friend WithEvents txtKeys As System.Windows.Forms.TextBox
  28.     Friend WithEvents Label2 As System.Windows.Forms.Label
  29.     Friend WithEvents btnSend As System.Windows.Forms.Button
  30.  
  31.     'Required by the Windows Form Designer
  32.     Private components As System.ComponentModel.Container
  33.  
  34.     'NOTE: The following procedure is required by the Windows Form Designer
  35.     'It can be modified using the Windows Form Designer.  
  36.     'Do not modify it using the code editor.
  37.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  38.         Me.Label1 = New System.Windows.Forms.Label()
  39.         Me.txtApplication = New System.Windows.Forms.TextBox()
  40.         Me.txtKeys = New System.Windows.Forms.TextBox()
  41.         Me.Label2 = New System.Windows.Forms.Label()
  42.         Me.btnSend = New System.Windows.Forms.Button()
  43.         Me.SuspendLayout()
  44.         '
  45.         'Label1
  46.         '
  47.         Me.Label1.Location = New System.Drawing.Point(16, 16)
  48.         Me.Label1.Name = "Label1"
  49.         Me.Label1.Size = New System.Drawing.Size(304, 24)
  50.         Me.Label1.TabIndex = 0
  51.         Me.Label1.Text = "Application Title "
  52.         '
  53.         'txtApplication
  54.         '
  55.         Me.txtApplication.Location = New System.Drawing.Point(16, 40)
  56.         Me.txtApplication.Name = "txtApplication"
  57.         Me.txtApplication.Size = New System.Drawing.Size(448, 24)
  58.         Me.txtApplication.TabIndex = 1
  59.         Me.txtApplication.Text = "Untitled - Notepad"
  60.         '
  61.         'txtKeys
  62.         '
  63.         Me.txtKeys.Location = New System.Drawing.Point(16, 104)
  64.         Me.txtKeys.Name = "txtKeys"
  65.         Me.txtKeys.Size = New System.Drawing.Size(448, 24)
  66.         Me.txtKeys.TabIndex = 1
  67.         Me.txtKeys.Text = ""
  68.         '
  69.         'Label2
  70.         '
  71.         Me.Label2.Location = New System.Drawing.Point(16, 80)
  72.         Me.Label2.Name = "Label2"
  73.         Me.Label2.Size = New System.Drawing.Size(304, 24)
  74.         Me.Label2.TabIndex = 0
  75.         Me.Label2.Text = "Keys"
  76.         '
  77.         'btnSend
  78.         '
  79.         Me.btnSend.Location = New System.Drawing.Point(168, 144)
  80.         Me.btnSend.Name = "btnSend"
  81.         Me.btnSend.Size = New System.Drawing.Size(96, 40)
  82.         Me.btnSend.TabIndex = 2
  83.         Me.btnSend.Text = "Send"
  84.         '
  85.         'SendKeysForm
  86.         '
  87.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  88.         Me.ClientSize = New System.Drawing.Size(488, 205)
  89.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnSend, Me.txtKeys, Me.Label2, Me.txtApplication, Me.Label1})
  90.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  91.         Me.Name = "SendKeysForm"
  92.         Me.Text = "SendKeysForm"
  93.         Me.ResumeLayout(False)
  94.  
  95.     End Sub
  96.  
  97. #End Region
  98.  
  99.     ' API declares
  100.     Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
  101.     Private Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Integer) As Integer
  102.  
  103.     ' send keys to selected app
  104.  
  105.     Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
  106.         SendKeysToApplication(txtApplication.Text, txtKeys.Text)
  107.     End Sub
  108.  
  109.     ' a reusable routine that sends specified keys to a given app
  110.  
  111.     Sub SendKeysToApplication(ByVal appTitle As String, ByVal keys As String)
  112.         ' Find the other application.
  113.         Dim hWnd As Integer = FindWindow(Nothing, appTitle)
  114.         ' Exit if not found.
  115.         If hWnd <= 0 Then
  116.             MessageBox.Show("Application not found", "Error", _
  117.                 MessageBoxButtons.OK, MessageBoxIcon.Error)
  118.             Exit Sub
  119.         End If
  120.         ' Make it the active application.
  121.         SetForegroundWindow(hWnd)
  122.         ' Send the keys and wait.
  123.         SendKeys.SendWait(keys)
  124.     End Sub
  125.  
  126. End Class
  127.